const utils = require("utils/util.js");
//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var sysInfo = wx.getSystemInfoSync();   
    this.globalData.sysInfo=sysInfo;
    this.correctLogin();
  },  
  onShow:function(){
    this.setMessageTask();
  },
  /**
   * 纠正登录
   */
  correctLogin:function(){
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: utils.apiUrl + '/shop/GetWxLoginInfo',
            data: {
              code: res.code
            },
            success: function (response) {
              var data = response.data;
              if (data.status == 0) {
                //
                var storeUserID=utils.getUserId();
                var onlineUserId = data.data.ShopID;
                //如果用户已注册，但线上凭证与本地存储不一致，则清除登录凭证
                if (onlineUserId > 0 && storeUserID!=onlineUserId) {
                  utils.setUserId("");                  
                } 
                else if(onlineUserId==0){//如果未注册，请清除登录凭证
                  utils.setUserId("");
                }
              }
            },
            fail: function () {
              
            },
            complete: function () {
             
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },

  setMessageTask:function(){
    var that=this;
      var interval=setInterval(function(){
        utils.getData(utils.apiUrl + "/Message/GetShopMessageCount", {}, function (res) {
          if (res.status == 0) {
            var nCount=parseInt(res.data);
            if(nCount>0){
              that.globalData.hasNewMessage=true;
              wx.setTabBarBadge({
                index: 2,
                text: res.data.toString()
              });
            }            
          }
        });
      },30000);
  },
  globalData: {
    userInfo: null,
    defaultCity:{
      name:"北京",
      id:201
    },
    sysInfo:null,
    bizTypeId:0,
    hasGoToLogin:false,
    hasNewMessage:false
  }
  
})